package com.example.hewei.smarttransport;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;

import com.example.hewei.smarttransport.Request.BaseRequest.RequestResult;
import com.example.hewei.smarttransport.Request.RequestThread;
import com.example.hewei.smarttransport.Util.IPUtil;

/**
 * Created by 何伟 on 2017/3/7.
 */

public class AppClient extends Application {
    private SharedPreferences mSharedPreferences;
    private String mServerIpStr = "";
    private Handler mHandler;
    private AlertDialog mDialog;
    private String email;//找回密码功能使用，用于存储Email邮箱地址

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mSharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        handleMessage();
    }

    /**
     * 接收消息进行处理
     *
     */
    private void handleMessage() {
        mHandler = new Handler(new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case RequestThread.MSG_REQUEST_RESULT:
                        RequestResult result = RequestResult.values()[msg.arg1];
                        RequestThread requestThread = (RequestThread) msg.obj;
                        if (requestThread != null) {
                            requestThread.hanlderResult(result);
                        }
                        if (result.equals(RequestResult.RESULT_NO_NET)) {
                            if (mDialog != null) {
                                if (!mDialog.isShowing()) {
                                    mDialog.setMessage(getString(R.string.no_net_work));
                                    mDialog.show();
                                }
                            }
                        }
                        if (result.equals(RequestResult.RESULT_FAIL)) {
                            if (mDialog != null) {
                                if (!mDialog.isShowing()) {
                                    mDialog.setMessage(getString(R.string.failed));
                                    mDialog.show();
                                }
                            }
                        }
                        break;
                }
                return false;
            }
        });
    }

    /**
     * 获取用户名
     *
     * @return 用户名
     */
    public String getUsername() {
        return mSharedPreferences.getString("username", "");
    }

    /**
     * 设置用户名
     *
     * @param username
     *            用户名
     */
    public void setUsername(String username) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("username", username);
        editor.commit();
    }

    /**
     * 获取密码
     *
     * @return 密码
     */
    public String getPassword() {
        return mSharedPreferences.getString("password", "");
    }

    /**
     * 设置密码
     *
     * @param password
     *            密码
     */
    public void setPassword(String password) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("password", password);
        editor.commit();
    }

    public Boolean getRemember(){
        return mSharedPreferences.getBoolean("remember", false);
    }

    public void setRemember(boolean remember){
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean("remember", remember);
        editor.commit();
    }
    /**
     * 获取Ip地址
     *
     * @return Ip地址
     */
    public String getServerIpStr() {
        mServerIpStr = readIpStr();
        if (mServerIpStr.equals("")) {
            try {
                mServerIpStr = IPUtil.getLocalIpAddress(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mServerIpStr;
    }

    /**
     * 保存用户输入的Ip
     *
     * @param serverIpStr
     *            用户输入Ip
     */
    public void setServerIpStr(String serverIpStr) {
        this.mServerIpStr = serverIpStr;
        saveIpStr(this.mServerIpStr);
    }

    /**
     * 从配置文件中获取存入的Ip
     *
     * @return Ip地址
     */
    private String readIpStr() {
        return mSharedPreferences.getString("IpStr", "");
    }

    /**
     * 将Ip地址存入配置文件中
     *
     * @param ipStr
     *            Ip地址
     */
    private void saveIpStr(String ipStr) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString("IpStr", ipStr);
        editor.commit();
    }

    /**
     * 获得Handler对象
     *
     * @return Handler对象
     */
    public Handler getHandler() {
        return mHandler;
    }

    /**
     * 显示对话框
     */
    private void showAlertDialog(Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(getString(R.string.prompt));
        builder.setPositiveButton(activity.getString(android.R.string.ok), null);
        mDialog = builder.create();
    }

    /**
     * 给Client设置Activity
     *
     * @param activity
     *            Activity
     */
    public void setActivity(Activity activity) {
        showAlertDialog(activity);
    }
}

